package com.voxel.sense.infomsg.module.map.monitor.traceRecord;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.com.common.app.BViewActivity;
import com.example.com.common.utils.L;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.voxel.sense.infomsg.R;
import cyy.example.com.facotry.constants.IntentConstants;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;
import com.voxel.sense.infomsg.view.titlebar.TextTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.model.api.response.MyTaskListInfoEntity;

public class TaskListActivity extends BViewActivity<TaskTraceContract.Presenter> implements TaskTraceContract.View {
    @BindView(R.id.titlebar)
    TextTitleBar titlebar;
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refresh_layout;

    private List<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> mTaskInfoList;
    private TaskAdapter mAdapter;


    public static void show(Activity context, List<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> taskInfoList) {
        if (taskInfoList == null || taskInfoList.size() < 1) {
            ToastUtil.showToastShort(Factory.app(), "暂无任务需要打卡");
            return;
        }
        Intent intent = new Intent(context, TaskListActivity.class);
        intent.putExtra(IntentConstants.OBJECT_TYPE, (ArrayList) taskInfoList);
        context.startActivityForResult(intent, IntentConstants.TRANCE_SELECT_TASK_ACTIVITY);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_task_list;
    }

    @Override
    protected TaskTraceContract.Presenter initPresenter() {
        return new TaskTracePresenter(this);
    }

    @Override
    protected boolean initArgs(Bundle bundle) {
        mTaskInfoList = (List<MyTaskListInfoEntity.BodyBean.TaskInfoListBean>) bundle.get(IntentConstants.OBJECT_TYPE);
        L.d("taskInfoList:" + mTaskInfoList.size());
        return super.initArgs(bundle);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        RecyclerView.LayoutManager manager = new LinearLayoutManager(this);
        recycle_view.setLayoutManager(manager);
        recycle_view.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider));
        MyTaskListInfoEntity.BodyBean.TaskInfoListBean taskInfoListBean = mTaskInfoList.get(0);
        for (int i= 0 ; i<20 ; i++){
            mTaskInfoList.add(taskInfoListBean);
        }
        mAdapter = new TaskAdapter(mTaskInfoList, new RecyclerAdapter.AdapterListener<MyTaskListInfoEntity.BodyBean.TaskInfoListBean>() {
            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, MyTaskListInfoEntity.BodyBean.TaskInfoListBean taskInfoListBean) {

            }

            @Override
            public void onItemLongClick(RecyclerAdapter.ViewHolder holder, MyTaskListInfoEntity.BodyBean.TaskInfoListBean taskInfoListBean) {

            }
        });

        recycle_view.setAdapter(mAdapter);

    }


    @Override
    protected void initData() {
        super.initData();
        titlebar.setTitleText("任务打卡");
    }

    @Override
    protected void initEven() {
        super.initEven();
        titlebar.setRightTextAndLis("确认", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int selectItem = mAdapter.getSelectItem();
                if (selectItem == -1) {
                    ToastUtil.showToastShort(Factory.app(), "请选择任务");
                } else {
                    Intent intent = new Intent();
                    List<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> items = mAdapter.getItems();
                    intent.putExtra(IntentConstants.OBJECT_TYPE, items.get(selectItem));
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });

        refresh_layout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                handleRefresh();
            }

            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                handleLoad();
            }
        });
    }

    private void handleLoad() {
        if (NetworkUtils.isConnected()) {
            mPresenter.doLoadMoreData();
        } else {
            showEmptyView(getString(R.string.network_unavailable),true);
            refresh_layout.finishLoadMore();
        }
    }


    private void handleRefresh() {
        if (NetworkUtils.isConnected()) {
            //重新刷新，需要改变状态，可以加载更多数据
            refreshData();
        } else {
            refresh_layout.finishRefresh();
            showEmptyView(getString(R.string.network_unavailable),true);
        }
    }

    private void refreshData() {
        refresh_layout.setNoMoreData(false);
        mPresenter.doLoadData();
    }


    @Override
    public RecyclerAdapter<MyTaskListInfoEntity.BodyBean.TaskInfoListBean> getRecyclerViewAdapter() {
        return mAdapter;
    }

    @Override
    public void onAdapterDataChanged() {

    }

    @Override
    public void showEmptyView(String desc, boolean btnVisible) {

    }

    @Override
    public void onShowNoMore(boolean b) {
        refresh_layout.setNoMoreData(true);
    }

    @Override
    public void onRefreshFinish() {
        refresh_layout.finishRefresh();
    }

    @Override
    public void onLoadMoreFinish() {
        refresh_layout.finishLoadMore();
    }
}
